<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Shortcode {
	protected $CI;
	function __construct()
	{
		$this->CI =& get_instance();
		//$this->CI->load->library('video/embedded_video');
	}
    function run($content)
	{	
		
		$regex = "/\[(.*?) id=(.*?)\]/";
		if(preg_match_all($regex, $content, $matches))
		{
			$countgallery=0;
			
			
			for($i = 0; $i < count($matches[1]); $i++)
			{
				$table = $matches[1][$i];
				$id	   = $matches[2][$i];
				$query = $this->CI->db->get_where($table, array('id' => $id));
				$result=$query->result_array();
				$shortdata="";
				$headpart="";
				//echo $result[0]['widget_type'];
				switch($result[0]['widget_type'])
				{
					case "gallery":
						
						if($countgallery==0)
						{
							//echo $countgallery;
							$headpart = '<link href="'.base_url().'assets/cms/css/lightbox.css" rel="stylesheet" />';
							$headpart .= '<script src="'.base_url().'assets/cms/js/lightbox-2.6.min.js"></script>';
							$shortdata.=$headpart;
						}
						else
						{
							$shortdata='';
						}
						
						$countgallery++;
						
						//return $shortdata;
						$shortdata.='<div class="image-row">';
						$shortdata.=	'<div class="image-set">';
						$query1 = $this->CI->db->get_where("gallery_images", array('gallery_id' => $id));
						
						foreach($query1->result_array() as $images)
						{
							$shortdata.=		'<a class="example-image-link" href="'.base_url().$images['filename'].'" data-lightbox="example-set" title="Click on the right side of the image to move forward."><img class="example-image" src="'.base_url().$images['filename'].'" alt="Plants: image 1 0f 4 thumb" width="150" height="150"/></a>';
						
						}
						$shortdata.=	'</div>';
						$shortdata.='</div>';

						break;
					case "custom":
						$query = $this->CI->db->get_where("widgets", array('id' => $id));
					
						foreach($query->result_array() as $custom)
						{
							$shortdata.=stripslashes($custom['widget_content']);
							
						}
						break;
					case "video":
						$query = $this->CI->db->get_where("widgets", array('id' => $id));
					
						foreach($query->result_array() as $custom)
						{
							$shortdata.=$this->CI->embedded_video->get_video(strip_tags($custom['widget_content']),array('height'=>$custom['widget_height']==0 ? 300 : $custom['widget_height'] ,'width'=>$custom['widget_width']==0 ? 400 : $custom['widget_width']));
						
						
						}
						break;
							
					
				}
				
				$content = str_replace($matches[0][$i], $shortdata, $content);
				
			}
			return $content;
		}
		else
		{
			return $content;
		}
		
		

		
    }

}